<?php
/**
 * 
 * Loader class. Loads nad return models.
 * @author Kuba
 *
 */
class Load {
    static private $_loaded = array();
    private function __construct() { }
    private function __clone() { }
    
    static function &model( $name ) {
        $name = strtolower( $name );
        if( isset( self::$_loaded[ 'models' ][ $name ] ) ) {
            return self::$_loaded[ 'models' ][ $name ];
        }else {
            if( file_exists( MODELS_PATH . $name . PHP_EX ) ) {
                include MODELS_PATH . $name . PHP_EX;
                $class_name = ucfirst( $name ) . '_Model';
                self::$_loaded[ 'models' ][ $name ] = new $class_name();
                if( !( self::$_loaded[ 'models' ][ $name ] instanceof Model ) ) {
                    unset( self::$_loaded[ 'models' ][ $name ] );
                    throw new FrameException( 'Model o nazwie ' . $name . ' nie jest poprawny' );
                    return;
                }
                return self::$_loaded[ 'models' ][ $name ];
            }else throw new FrameException( 'Nie ma modelu o nazwie ' . $name );
        }
    }
    
    static function &plugin( $name ) {
        $name = strtolower( $name );
        if( isset( self::$_loaded[ 'plugins' ][ $name ] ) ) {
            return self::$_loaded[ 'plugins' ][ $name ];
        }else {
            if( file_exists( PLUGINS_PATH . $name . PHP_EX ) ) {
                include PLUGINS_PATH . $name . PHP_EX;
                $class_name = ucfirst( $name ) . '_Plugin';
                self::$_loaded[ 'plugins' ][ $name ] = new $class_name();
                return self::$_loaded[ 'plugins' ][ $name ];
            }else throw new FrameException( 'Nie ma pluginu o nazwie ' . $name );
        }
    }
}